{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0d69250d",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Downloader(object):\n",
    "    \"\"\"\n",
    "    一个异步下载器，可以对代理源异步抓取，但是容易被BAN。\n",
    "    \"\"\"\n",
    "\n",
    "    def __init__(self, urls):\n",
    "        self.urls = urls\n",
    "        self._htmls = []\n",
    "\n",
    "    async def download_single_page(self, url): #执行完成后返回协程对象，后经过Task包装\n",
    "        async with aiohttp.ClientSession() as session:\n",
    "            async with session.get(url) as resp:\n",
    "                self._htmls.append(await resp.text())\n",
    "\n",
    "    def download(self):\n",
    "        loop = asyncio.get_event_loop()\n",
    "        tasks = [self.download_single_page(url) for url in self.urls]\n",
    "        loop.run_until_complete(asyncio.wait(tasks))\n",
    "\n",
    "    @property\n",
    "    def htmls(self):\n",
    "        self.download()\n",
    "        return self._htmls\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26700396",
   "metadata": {},
   "outputs": [],
   "source": [
    "async with session.get(self.test_api, proxy=real_proxy, timeout=get_proxy_timeout) as response:\n",
    "    if response.status == 200:\n",
    "        self._conn.put(proxy)\n",
    "        print('Valid proxy', proxy)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
